package wiki.zimo.servlet.backstage;

import org.apache.commons.beanutils.BeanUtils;
import wiki.zimo.constant.Constant;
import wiki.zimo.dao.OrderDao;
import wiki.zimo.dao.UserDao;
import wiki.zimo.domain.Order;
import wiki.zimo.domain.User;
import wiki.zimo.servlet.BaseServlet;
import wiki.zimo.utils.JsonUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/10 12:40
 * @description: 处理后台用户管理的servlet
 */
@WebServlet(value = "/userManage")
public class UserManageServlet extends BaseServlet {

    UserDao userDao = new UserDao();
    OrderDao orderDao = new OrderDao();

    /**
     * @auther: 子墨
     * @date: 2018/11/10 12:45
     * @describe: 用户列表页
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String userListPage(HttpServletRequest request, HttpServletResponse response) {
        return Constant.URL_BACKSTAGE_USER_LIST;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/10 14:58
     * @describe: 修改用户信息页
     * @param: []
     * @return: java.lang.String
     * @version v1.0
     */
    public String editUserPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uid = request.getParameter("uid");
        User user = userDao.findOneById(uid);
        request.setAttribute(Constant.USER_LOG_ON, user);
        return Constant.URL_BACKSTAGE_USER_ADD;
    }


    /**
     * @auther: 子墨
     * @date: 2018/11/10 12:53
     * @describe: 获取用户列表信息接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getAllUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<User> users = userDao.findAll();
        String json = JsonUtils.listToJson(users);
        response.getWriter().print(json);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/11 11:15
     * @describe: 更新用户信息接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uid = request.getParameter("uid");
        User user = userDao.findOneById(uid);
        BeanUtils.populate(user, request.getParameterMap());

        userDao.update(user);
        request.setAttribute(Constant.MSG, "修改用户“" + user.getUsername() + "”成功");
        return userListPage(request, response);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/10 14:59
     * @describe: 删除用户信息接口
     * @param: []
     * @return: java.lang.String
     * @version v1.0
     */
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uid = request.getParameter("uid");
        User user = userDao.findOneById(uid);
        if (user != null) {
            userDao.delete(user);
            List<Order> orders = orderDao.findOrdersByUid(user.getUid());
            for (Order o : orders) {
                orderDao.delete(o);
            }
            request.setAttribute(Constant.MSG, "删除用户“" + user.getUsername() + "”成功");
        }
        return userListPage(request, response);
    }
}
